/**********************************************************************************************************************
 * @file    nmea_gsa.c
 * @author  Queclink Jason.huang
 * @date    2019-01-02
 * @brief   NMEA-GSA frame source.
 *
 * Copyright (C) 2018 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 *
 * @attention
 *
 * @usage
 *
 *********************************************************************************************************************/

/* includes ---------------------------------------------------------------------------------------------------------*/
#include "nmea.h"
#include "qlcommon.h"
#include <task_gnss.h>
#include <gnss_info_process.h>


bool gsa_update = true;         /* 可用卫星更新标志 */
/*
 * @brief  GSA消息解析
 * @note
 * @param  argc: [I], 消息分割后字符串数量
 *         argv: [I], 字符串地址列表
 * @retval none
 */
void nmea_gsa_parse(int argc, char *argv[])
{
    int type = 0; /*定位类型  0-未定位,2-2D定位,3-3D定位*/
    nmea_protocol_parse_t *nmea_ptr;
    gnss_task_manage_t *gps_device;
    static struct usable_sv_t usable_sv = {
        .cnt = 0,
    };

    gps_device = get_gnss_manage();
    nmea_ptr = get_nmea_manage_context();
    if (NULL == nmea_ptr)
        return;

    nmea_ptr->recv_nmea_msg |= GSA_MASK;
    type = atoi(argv[2]);
    if (type == 2) {
        gps_device->anti_drift_m.cnt_2d_fix++;
        nmea_ptr->raw_info.state = FIX_OK;  /* 2D 定位点 */
        nmea_ptr->stream_valid_mask |= GSA_MASK;
        nmea_ptr->raw_info.fixmode = FIXMODE_2D;
    } else if (type == 3){
        gps_device->anti_drift_m.cnt_2d_fix = 0;
        nmea_ptr->raw_info.state = FIX_OK;  /* 3D 定位点 */
        nmea_ptr->stream_valid_mask |= GSA_MASK;
        nmea_ptr->raw_info.fixmode = FIXMODE_3D;
    } else {                                /* 未定位 */
        nmea_ptr->stream_valid_mask &= (~GSA_MASK);
        gps_device->anti_drift_m.cnt_2d_fix = 0;
    }

    if(!gps_device->gnss_info.is_uart_rx_ok)
        gps_device->gnss_info.is_uart_rx_ok = true;

    if (gsa_update) {
        gsa_update = false;
        gnss_usable_sv_update(&usable_sv);
        usable_sv.cnt = 0;
    }

    for (int i=0; i<12; i++) {
        if (strlen(argv[3+i]) > 0 && usable_sv.cnt < USABLE_SV_MAX) {
            usable_sv.sv_num[usable_sv.cnt] = (unsigned char)(atoi(argv[3+i]));
            usable_sv.cnt++;
        }
    }
}
